<?
include_once 'include/processes.php';
$Login_Process = new Login_Process;
$Login_Process->check_status($_SERVER['SCRIPT_NAME']);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Crash Report</title>
<link href="include/style.css" rel="stylesheet" type="text/css">
<script language="javascript">
function AssignUser(dumpId)
{
	var idx = document.getElementById(dumpId).selectedIndex;
	var newUserName = document.getElementById(dumpId).options[idx].text;
}
</script>
</head>

<body>

<?php 


$status_name[0] = '--';
$status_name[1] = 'Resolved';
$status_name[2] = 'Working on it..';
$status_name[3] = 'Thinking fixed';
$status_name[4] = 'Surrender';

$status_icon[1] = 'http://www.butkovic.net/pictures/smileys/yeah.gif';
$status_icon[2] = 'http://www.butkovic.net/pictures/smileys/confus1.gif';
$status_icon[4] = 'http://www.butkovic.net/pictures/smileys/surrender.gif';



$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
if (!$conn) {
	die("DB error - ".mysql_error());
}
if (!mysql_select_db(DBNAME, $conn)) {
	die("DB error - ".mysql_error());
}

$queryStr = "select userid, username from users";
$result = mysql_query($queryStr);
if (!$result) {
	die("DB error 2 - ".mysql_error());
}

$numRows = mysql_num_rows($result);
$userMap = array("--" => -1);
while ($row = mysql_fetch_assoc($result)) {
	$userMap[$row['userid']] = $row['username'];
	//$userMap[$row['username']] = $row['userid'];
	//echo "<p>username=".$row['username'].", userid=".$row['userid']."</p>";
}


if ($_GET['crash_id']) {
	echo '<div class=center>';

	$crash_id = stripslashes($_GET['crash_id']);
	echo 'crash id : '.$crash_id.'<br>';
	$result = mysql_query("select * from dump_files where caddr_id = '$crash_id' order by upload_date DESC");

	$num = mysql_num_rows($result);

	echo "dump count : {$num}<br>";

	while ($row = mysql_fetch_assoc($result)) {
		$dumpFilePath = $row['path'];
		$dumpFileName = basename($dumpFilePath);
		$dumpLink = '<a href="download.php?f='.$dumpFileName.'">dump</a>';

		$crashLink = '';
		$crashFilePath = preg_replace('"\.dmp$"', '.crash', $dumpFilePath);
		if (file_exists($crashFilePath)) {
			$crashFileName = basename($crashFilePath);
			$crashLink = '<a href="download.php?f='.$crashFileName.'">crash</a>';
		}

		$logLink = '(no log)';
		$logFilePath = preg_replace('"\.dmp$"', '.log', $dumpFilePath);
		if (file_exists($logFilePath)) {
			$logFileName = basename($logFilePath);
			$logLink = '<a href="download.php?f='.$logFileName.'">log</a>';
		}
		

		echo "[{$row[id]}] : $dumpLink, $crashLink, $logLink ({$row[upload_date]})";

		echo '<br>';
	}

	echo '</div>';

	exit;
}


$hour_limit = 24;
$limit = 100;

if ($_GET['hour_limit']) {
	$hour_limit = $_GET['hour_limit'];
}
if ($_GET['limit']) {
	$limit = $_GET['limit'];
}


$q_time_limit = "";
if ($hour_limit) {
	$now = date("Y-m-d H:i:s", time() - 3600 * $hour_limit);
	$q_time_limit = " AND d.upload_date > '$now' ";
}


$link_param = "hour_limit=$hour_limit&limit=$limit";



//$queryStr = "SELECT d.id AS dump_id, p.name AS process_name, d.uploader AS uploader, d.upload_date AS upload_date, d.path AS dump_file, u.user_name FROM process p, dump_files d, caddr c  WHERE d.process = p.id AND c.id == d.caddr_id ORDER BY d.upload_date DESC LIMIT 100";
$queryStr = "SELECT d.id AS dump_id, p.name AS process_name, d.uploader AS uploader, d.path AS dump_file, (select username from users where userid = c.user_id) as user_name, c.count AS count, c.id as cid, c.comment as comment, c.status as status, c.address as addr , c.modified_user_id as modified_user_id, c.modifed_time as modifed_time FROM process p, dump_files d, caddr c, users u WHERE d.process = p.id AND c.id = d.caddr_id $q_time_limit GROUP BY c.id ORDER BY c.count DESC, d.upload_date DESC LIMIT $limit";
$result = mysql_query($queryStr);
if (!$result) {
	die("DB error 1 - ".mysql_error());
}
$numRows = mysql_num_rows($result);


echo "<div class=\"center\">";
echo "<h1>Top $limit crashes";
if ($hour_limit) {
	echo " for last $hour_limit hours";
}
echo "</h1>\n";
echo "</div>";

echo '<div class=center>';
echo "<a href=view_dumps.php?$link_param&hour_limit=0>All</a> | ";
echo "<a href=view_dumps.php?$link_param&hour_limit=6>Last 6Hour</a> | ";
echo "<a href=view_dumps.php?$link_param&hour_limit=12>Last 12Hour</a> | ";
echo "<a href=view_dumps.php?$link_param&hour_limit=24>Last 24Hour</a>";
echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
echo "<a href=view_dumps.php?$link_param&limit=50>Top 50</a> | ";
echo "<a href=view_dumps.php?$link_param&limit=100>Top 100</a> | ";
echo "<a href=view_dumps.php?$link_param&limit=500>Top 500</a>";
echo '</div>';
echo '<br>';




echo "<div class=\"center\">";
echo '<table border=0 width=1% class=none><tr style="background-color:white;"><td width=10%>';
echo "<table border=0 style='border-style:solid; border-spacing:1; border-width:0; background-color:#82b0a6;'>";

$numFields = mysql_num_fields($result);

echo "<tr bgcolor='white' style='margin:1px;'>";
#for ($i = 0; $i < $numFields; ++$i) {
#	$meta = mysql_fetch_field($result, $i);
#	echo "<th>{$meta->name}</th>";
#}
#echo "<th>crash_file</th>";
echo '<th>cid</th><th>process</th><th>address</th><th>date</th><th>count</th><th>dump file</th><th>crash file</th><th colspan=2>status</th><th>assigned</th><th>comment</th><th></th><th>modified by</th><th>modified time</th>';
echo "</tr>";


$rowColors[0] = '#eff5f3';
$rowColors[1] = 'white';
$count = 0;

while ($row = mysql_fetch_assoc($result)) {
	$count++;

	$fontColor = '';
	$color = $rowColors[$count % count($rowColors)];

	if ($row['status'] == 1) {
		$fontColor = 'color:#888888;';
	}

	$last_dump_time = @mysql_fetch_assoc(mysql_query("select upload_date from dump_files where caddr_id = '{$row[cid]}' order by upload_date desc limit 1"));

	echo "<tr bgcolor='$color' style='padding:1px;$fontColor'>";
	echo "<form name='modify_{$row['dump_id']}' action='modify.php' method='post'>";
	echo "<input type=hidden name=id value={$row['dump_id']}>";
	echo "<input type=hidden name=cid value={$row['cid']}>";
	echo "<td>".$row['cid']."</td>";
	echo "<td>".$row['process_name']."</td>";
	$addrLink = '<a href="view_comments.php?crash_addr='.$row['addr'].'">'.$row['addr'].'</a>';
	//echo "<td>".$row['addr']."</td>";
	echo "<td>".$addrLink."</td>";
	echo "<td>".$last_dump_time['upload_date']."</td>";
	echo "<td><a href='view_dumps.php?crash_id={$row['cid']}'>".$row['count']."</a></td>";

	$dumpFilePath = $row['dump_file'];
	$dumpFileName = basename($dumpFilePath);
	$dumpLink = '<a href="download.php?f='.$dumpFileName.'">down</a>';
	echo "<td>".$dumpLink."</td>";

	$crashFilePath = preg_replace('"\.dmp$"', '.crash', $dumpFilePath);
	if (file_exists($crashFilePath)) {
		$crashFileName = basename($crashFilePath);
		$crashLink = '<a href="download.php?f='.$crashFileName.'">down</a>';
		echo "<td>".$crashLink."</td>";
	} else {
		echo "<td>No crash file</td>";
	}


	echo '<td>';
	if ($status_icon[$row[status]]) {
		echo '<img src='.$status_icon[$row[status]].'>';
	}
	echo '</td><td><select name=status>';
	foreach ($status_name as $k => $v) {
		if ($row['status'] == $k) {
			echo '<option value="'.$k.'" selected="true">'.$v.'</option>';
		} else {
			echo '<option value="'.$k.'">'.$v.'</option>';
		}
	}

	echo'</td>';


	$userName = stripslashes($row['user_name']);
	echo "<td>";
	echo '<select name="user_id">';
	$cbId += 1;	
	foreach ($userMap as $k => $v) {
		if ($userName && $userName == $v) {
			echo '<option value="'.$k.'" selected="true">'.$v.'</option>';
		} else {
			echo '<option value="'.$k.'">'.$v.'</option>';
		}
	}
	echo "</select>";
	echo "</td>";


	$comment = stripslashes($row['comment']);

	//echo'<td>'.$row['comment'].'</td>';
	echo "<td><input type=text name=comment maxlength=512 size=30 value='$comment'></td>";
	echo "<td><input type=submit value='apply' width=20 style='font-size:8pt;'></td>";
	echo '</form>';

	echo '<td>';
	if ($row[modified_user_id]) {
		echo $userMap[$row[modified_user_id]];
	}
	echo '</td>';
	echo "<td>{$row[modifed_time]}</td>";

	echo "</tr>";
}

echo "</table>";
echo '</td><td width=1% valign=top style="padding-left:20px;">';

echo '<table width=100px border=0 bgcolor=#d4d4d4>';
echo '<tr bgcolor=#EEEEEE><td>Date</td><td>Dump</td><td>Uniq. Addr.</td></tr>';

$start_h = 10;
$end_h = 26;

$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));

for ($i = 0; $i < 7; $i++) {
	$start = $today - ($i * 3600 * 24) + $start_h;
	$end = $today - (($i - 1) * 3600 * 24) + $end_h;

	$day = date('d', $start);
	$month = date('m', $start);

	if ($day < 24) {
		break;
	}

	$start = date('Y-m-d H:i:s', $start);
	$end = date('Y-m-d H:i:s', $end);

	$dumps = @mysql_fetch_assoc(mysql_query("select count(*) as count from dump_files where upload_date > '$start' and upload_date < '$end'"));
	$crashes = @mysql_fetch_assoc(mysql_query("select count(DISTINCT caddr_id) as count from dump_files where upload_date > '$start' and upload_date < '$end'"));

	echo "<tr bgcolor=white><td>$month/$day</td><td>{$dumps[count]}</td><td>{$crashes[count]}</td></tr>";

}
echo '</table>';

echo '</td></tr></table>';
echo "</div>";

/*
echo '<div>';
echo '<form name="myfrom" action="">';
echo '<input type="button" name="Commit user assignments" value="Commit Users" onClick="commitUserAssignments()"></div>';
echo '</form>';
echo '</div>';
*/

?>

<div class="center">
<p>-----------------------------------</p>

<div class="center">
<a href="main.php">Back to main</a> | <a href="qa_report.php">QA Report</a>
</div>

</body>
</html>

